﻿using System.IO;
using Microsoft.VisualStudio.DebuggerVisualizers;

namespace EntityFrameworkVisualizer.VisualizerObjectSources
{
    /// <summary>
    /// Base class for visualizers.
    /// </summary>
    /// <typeparam name="T">Object type.</typeparam>
    public abstract class QueryVisualizerObjectSource<T> : VisualizerObjectSource
    {
        /// <summary>
        /// Writes data to outgoing data stream.
        /// </summary>
        /// <param name="target">Object being visualized.</param>
        /// <param name="outgoingData">Outgoing data stream.</param>
        public override void GetData(object target, Stream outgoingData)
        {
            string sql = GetSql((T)target);
            var sw = new StreamWriter(outgoingData);
            sw.Write(sql);
            sw.Flush();
        }

        /// <summary>
        /// Gets SQL source code.
        /// </summary>
        /// <param name="target">Target object.</param>
        /// <returns>SQL source code.</returns>
        protected abstract string GetSql(T target);
    }
}
